package util;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextLayout;
import java.awt.geom.Point2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;

import java.text.AttributedCharacterIterator;
import java.text.AttributedString;

import javax.swing.text.BadLocationException;
import javax.swing.text.Document;


/**
 * 
 */
public class PrintItem implements Printable {
    private final AttributedString[] attributedItems;
    public final static String EMPTY_PARAG = "    ";


    /**
     * The constructor.
     * @param Object[] items
     */
    public PrintItem(Object[] items) {
        attributedItems = new AttributedString[items.length];
        for (int i=0; i < items.length; i++) {
            this.attributedItems[i] = new AttributedString((String)items[i]);
        }
    }

    /**
     * The constructor.
     * @param doc
     */
    public PrintItem(Document doc) {
        String docText = null;
        try {
            docText = doc.getText(0, doc.getLength());
        }catch (BadLocationException e) {}
        String[] texts =  docText.split("\n");
        attributedItems = new AttributedString[texts.length];
        for (int i=0; i < texts.length; i++) {
            if(texts[i].length() == 0)
                this.attributedItems[i] = new AttributedString(" \n");
            else
                this.attributedItems[i] = new AttributedString(texts[i]);
        }
        
    }

    /**
     * 
     * @param Graphics graphics
     * @param PageFormat pageFormat
     * @param int pageIndex
     * @return int
     */
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
        if (pageIndex >= 1) {
            return Printable.NO_SUCH_PAGE;
        }
 
        Graphics2D graphics2d = (Graphics2D)graphics;
        graphics2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
        graphics2d.setPaint(Color.black);
        Point2D.Float pen = new Point2D.Float();
 
        for(int i=0; i < attributedItems.length; i++) {
            printText(graphics2d, pageFormat, pen, attributedItems[i]);
        }
 
        return Printable.PAGE_EXISTS;
    }


    /**
     * 
     * @param Graphics2D graphics2d
     * @param PageFormat pageFormat
     * @param Point2D.Float pen
     * @param AttributedString text
     */
    private void printText(Graphics2D graphics2d, PageFormat pageFormat, Point2D.Float pen, AttributedString text) {
        AttributedCharacterIterator charIterator = text.getIterator();
        LineBreakMeasurer measurer = new LineBreakMeasurer(charIterator, graphics2d.getFontRenderContext());
        float wrappingWidth = (float) pageFormat.getImageableWidth();
        while (measurer.getPosition() < charIterator.getEndIndex()) {
            TextLayout layout = measurer.nextLayout(wrappingWidth);
            pen.y += layout.getAscent();
            // if true --> 0
            float dx = layout.isLeftToRight()? 0 : (wrappingWidth - layout.getAdvance());
            layout.draw(graphics2d, pen.x + dx, pen.y);
            pen.y += layout.getDescent() + layout.getLeading();
        }
    }
}
